package cn.ztuo.bitrade.job;

import cn.ztuo.bitrade.constant.SysConstant;
import cn.ztuo.bitrade.entity.ContractPriceControl;
import cn.ztuo.bitrade.service.ContractPriceControlService;
import cn.ztuo.bitrade.util.RedisUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

/**
 * @description: WaitingOrderPushJob
 * @author: fiwork.net
 * @create: 2019/04/27 17:00
 */
@Slf4j
@Component
public class ContractPriceControlJob {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ContractPriceControlService priceControlService;
    @Resource(name = "contractPriceControlSymbols")
    private Map<String, TreeMap<Date, ContractPriceControl>> contractPriceControlSymbols;

    @Scheduled(fixedRate = 500)
    public void pushTrade(){
        for (String keySet:contractPriceControlSymbols.keySet()){
            TreeMap<Date,ContractPriceControl> map = contractPriceControlSymbols.get(keySet);
            Date now = new Date();
            String key = SysConstant.CONTRACT_PRICE_CONTROL+keySet+":";
            synchronized (map){
                if(map.size()>0){
                    Iterator titer=map.entrySet().iterator();
                    while(titer.hasNext()) {
                        Map.Entry ent=(Map.Entry )titer.next();
                        Date keyMap=(Date)ent.getKey();
                        //过时失效后从内存中删除
                        if(map.get(keyMap).getEndTime().before(now)){
                            //如果当前时间 不满足控制器列表第一个 控制器的开始时间以及结束时间 将该对象从redis删除以及从内容中删除
                            if(redisUtil.hasKey(key)){
                                redisUtil.delete(key);
                            }
                            map.get(keyMap).setStatus(3);
                            priceControlService.saveAndFlush(map.get(keyMap));
                            map.remove(map.get(keyMap).getStartTime());
                        }else{
                            //如果当前时间 满足控制器列表第一个 控制器的开始时间以及结束时间 将该对象存入redis
                            Date now1 = new Date();
                            if(map.get(keyMap).getStatus()==0
                                    &&map.get(keyMap).getStartTime().before(now1)
                                    &&map.get(keyMap).getEndTime().after(now1)){
                                redisUtil.set(key,map.get(keyMap));
                                map.get(keyMap).setStatus(1);
                                break;
                            }
                        }
                    }
                }else{
                    if(redisUtil.hasKey(key)){
                        redisUtil.delete(key);
                    }
                }
            }
        }
    }
}
